﻿// Version:1
// Author:Sanket Naik
// Date:July-21-2010 
// Comments:
// 
// 
// 



using System;
using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace ManagedServices.Common.Utility
{
    /// <summary>
    /// Utility class helping Serialization and DeSerialization
    /// </summary>
    public class SerializationHelper
    {
        /// <summary>
        /// Serializes an instance and returns the serialized XML string using XML Serializer
        /// </summary>
        /// <typeparam name="TTypeToSerialize">the type to serialize</typeparam>
        /// <param name="instance">the instance of the type to serialize</param>
        /// <returns>the serialized output</returns>
        public static string XmlSerialize<TTypeToSerialize>(TTypeToSerialize instance) where TTypeToSerialize : class
        {
            XmlSerializer ser = new XmlSerializer(typeof(TTypeToSerialize));
            MemoryStream mStream = new MemoryStream();
            ser.Serialize(mStream, instance);
            mStream.Position = 0;
            using (StreamReader sRdr = new StreamReader(mStream))
            {
                return sRdr.ReadToEnd();
            }
        }

        /// <summary>
        /// DeSerializes an instance from the serialized XML string using XML Serializer
        /// </summary>
        /// <typeparam name="TTypeToDeserialize">the type to deserialize</typeparam>
        /// <param name="serializedInstance">the serialized input</param>
        /// <returns>the deserialized instance </returns>
        public static TTypeToDeserialize XmlDeSerialize<TTypeToDeserialize>(string serializedInstance) where TTypeToDeserialize : class
        {
            XmlSerializer ser = new XmlSerializer(typeof(TTypeToDeserialize));
            MemoryStream mStream = new MemoryStream();
            StreamWriter sw = new StreamWriter(mStream);
            sw.Write(serializedInstance);
            sw.Flush();
            mStream.Position = 0;
            return (TTypeToDeserialize)ser.Deserialize(mStream);
        }

        /// <summary>
        /// Serializes an instance and returns the serialized string using DataContractSerializer
        /// </summary>
        /// <typeparam name="TTypeToSerialize">the type to serialize</typeparam>
        /// <param name="instance">the instance of the type to serialize</param>
        /// <returns>the serialized output</returns>
        public static string Serialize<TTypeToSerialize>(TTypeToSerialize instance) where TTypeToSerialize: class
        {
            DataContractSerializer ser = new DataContractSerializer(typeof(TTypeToSerialize));
            MemoryStream mStream = new MemoryStream();
            ser.WriteObject(mStream, instance);
            mStream.Position = 0;
            using (StreamReader sRdr = new StreamReader(mStream))
            {
                return sRdr.ReadToEnd();
            }
        }

        /// <summary>
        /// DeSerializes an instance from the serialized string
        /// </summary>
        /// <typeparam name="TTypeToDeserialize">the type to deserialize</typeparam>
        /// <param name="serializedInstance">the serialized input</param>
        /// <returns>the deserialized instance </returns>
        public static TTypeToDeserialize DeSerialize<TTypeToDeserialize>(string serializedInstance) where TTypeToDeserialize:class
        {
            DataContractSerializer ser = new DataContractSerializer(typeof(TTypeToDeserialize));
            MemoryStream mStream = new MemoryStream();
            StreamWriter sw = new StreamWriter(mStream);
            sw.Write(serializedInstance);
            sw.Flush();
            mStream.Position = 0;
            return (TTypeToDeserialize)ser.ReadObject(mStream);
        }
    }
}
